﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using WindowsFormsApp1.SQlite;

namespace WindowsFormsApp1
{
    public delegate void delegatePersionModelCallback(PersonModel persionModel);
    public partial class frmEmployee : Form
    {
        public delegatePersionModelCallback Callback { get; set; }
        public event EventHandler addResumeClick;
        public event EventHandler deleteResumeClick;
        public frmEmployee()
        {
            InitializeComponent();
            listViewFindResult.SelectedIndexChanged += new EventHandler(listView_SelectedIndexChanged);
        }

        private void btnAddResume_Click(object sender, EventArgs e)
        {
            if(addResumeClick != null)
            {
                addResumeClick?.Invoke(this, EventArgs.Empty);
            }
        }

        private void btnFind_Click(object sender, EventArgs e)
        {
            if (tbInputName.Text.Trim() == string.Empty) return;
            listViewFindResult.Items.Clear();
            List<PersonModel> persionModels;
            bool isNumeric = Regex.IsMatch(tbInputName.Text.Trim(), @"^\d+$");
            if (isNumeric)
                persionModels= DbOperate.Instance.GetPersonByName("", tbInputName.Text.Trim());
            else
                persionModels = DbOperate.Instance.GetPersonByName(tbInputName.Text.Trim());
            
            if (persionModels is null) return;

            foreach(var p in persionModels)
            {
                ListViewItem item = new ListViewItem(p.name);
                item.SubItems.Add(p.cardNo);
                item.Tag = p.guid;
                listViewFindResult.Items.Add(item);
            }

        }

        private void listView_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listViewFindResult.SelectedItems.Count > 0)
            {
                ListViewItem selectedItem = listViewFindResult.SelectedItems[0];
                PersonModel persionModel = DbOperate.Instance.GetPersonByGuid(selectedItem.Tag.ToString());
                if (persionModel is null) return;
                Callback?.Invoke(persionModel);



            }
        }

        private void btnDeleteResume_Click(object sender, EventArgs e)
        {
            if (deleteResumeClick != null)
            {
                deleteResumeClick?.Invoke(this, EventArgs.Empty);
            }
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            btnAddResume_Click(sender, e);
        }
    }
}
